/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.zul.Tab;

/**
 *
 * @author kasidit
 */
public class ValidateManager {

    private static Log LOG = LogFactory.getLog(ValidateManager.class);
    private String message = ResourceUtil.getLabel("validate.message.default");
    private Map<Integer, String> messages = new HashMap<Integer, String>();
    private Map<String, Boolean> mapTab = new HashMap<String, Boolean>();
    private Boolean value = true;
    private static final String INVALID = "background: red;";
    private static final String VALID = null;

    public void add(Validation validation) {
        if (validation != null) {
            Boolean value = validation.getValue();
            if (!value) {
                messages.put(validation.getId(), validation.getMessage());
            }
            if (this.value == true) {
                this.value = value;
            }
        }
    }

    public void add(Validation validation, Tab tab) {
        add(validation);
        Boolean value = validation.getValue();
        Boolean tabValue = mapTab.get(tab.getId());
        if (tabValue == null) {
            mapTab.put(tab.getId(), value);
        } else {
            if (tabValue) {
                if (!value) {
                    mapTab.put(tab.getId(), value);
                }
            }
        }
        tabValue = mapTab.get(tab.getId());
        if (tabValue) {
            tab.setStyle(VALID);
        } else {
            tab.setStyle(INVALID);
        }
    }

    public Boolean getValue() {
        return value;
    }

    public void setValue(Boolean value) {
        this.value = value;
    }

    public String getMessage() {
        return this.message;
    }

    public List<String> getMessages() {
        List<String> list = new ArrayList<String>(messages.values());
        return list;
    }

    public String getMessageTemplate() {
        StringBuilder sb = new StringBuilder();
        List<String> list = new ArrayList<String>(messages.values());
        sb.append(list.get(0));
//        for(int i=0;i<list.size();i++){
//
//            //sb.append(i+1);
//            //sb.append(". ");
//            //sb.append(list.get(i));
//            //sb.append("\n");
//        }
        return sb.toString();
    }

    public String getMessageTemplateAll() {
        StringBuilder sb = new StringBuilder();
        List<String> list = new ArrayList<String>(messages.values());
        sb.append(list.get(0));
        for (int i = 0; i < list.size(); i++) {

            sb.append(i + 1);
            sb.append(". ");
            sb.append(list.get(i));
            sb.append("\n");
        }
//        for(int i=0;i<list.size();i++){
//
//            //sb.append(i+1);
//            //sb.append(". ");
//            //sb.append(list.get(i));
//            //sb.append("\n");
//        }
        return sb.toString();
    }
}
